package com.cz.run.bumng;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cz.run.beans.mysql.BumngUser;
import com.cz.run.beans.mysql.News;
import com.cz.run.bumng.model.PageModel;
import com.cz.run.bumng.model.Result;
import com.cz.run.service.INewsService;
import com.cz.run.utils.FileUtil;
import com.cz.run.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by deior on 2018/6/9.
 */
@Controller
@RequestMapping("/bumng")
public class NewsController {

    @Autowired
    private INewsService iNewsServicel;

    @Value("${spring.html.path}")
    private String htmls;

    private static  final String url ="http://120.79.249.39:8092";

    @RequestMapping(value ="/news.htm",method = RequestMethod.GET)
    public String news(HttpServletRequest request, @ModelAttribute BumngUser bumngUser, Model model){

        BumngUser bumngUser1 = (BumngUser) request.getSession().getAttribute("bumngUser");

        if(null==bumngUser1){
            return "login";
        }
        model.addAttribute("bumngUser",bumngUser1);

        String author =request.getParameter("author");
        model.addAttribute("author",author);

        PageModel pageModel =  PageUtil.getPageData(request);

        EntityWrapper<News> ew = new EntityWrapper<>();
        ew.where(StringUtils.hasLength(author),"author = {0}",author).
                orderBy("create_time desc limit "+pageModel.getPageNum()+","+pageModel.getPageSize());;
        List<News> news =  iNewsServicel.selectList(ew);

        EntityWrapper<News> ew1 = new EntityWrapper<>();
        ew1.where("1 = 1").and(StringUtils.hasLength(author),"author = {0}",author);;
        int count =  iNewsServicel.selectCount(ew1);

        model.addAttribute("pageTotal",count%pageModel.getPageSize()>0?count/10+1:count/10);
        model.addAttribute("pageNum",(pageModel.getPageNum())/10+1);

        model.addAttribute("news",news);

        return "news";
    }

    @RequestMapping(value = "editNews.htm")
    public String editNews(HttpServletRequest request, Model model) {
        BumngUser bumngUser1 = (BumngUser) request.getSession().getAttribute("bumngUser");

        if(null==bumngUser1){
            return "login";
        }
        model.addAttribute("bumngUser",bumngUser1);
        News news = new News();
        String id =request.getParameter("id");
        if(StringUtils.hasLength(id)){
            EntityWrapper<News> ew = new EntityWrapper<>();
            ew.where("id = {0}",id);
             news =iNewsServicel.selectOne(ew);

            model.addAttribute("type","1");
        }else{
            model.addAttribute("type","2");
        }

        if(StringUtils.hasLength(news.getHtml()))
        news.setHtml(HtmlUtils.htmlUnescape(news.getHtml()));
        model.addAttribute("news",news);

        return "editNews";
    }


    @RequestMapping(value = "addNews.do", method = RequestMethod.POST)
    @ResponseBody
    public Object addNews(HttpServletRequest request, @ModelAttribute News news, Model model) {


        if(StringUtils.isEmpty(news.getAuthor())||StringUtils.isEmpty(news.getContent())||StringUtils.isEmpty(news.getSummary())||StringUtils.isEmpty(news.getImage())||
        StringUtils.isEmpty(news.getTitle())||StringUtils.isEmpty(news.getType())){

            return  new Result(false,"参数不能为空");
        }


        String html = "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>"+news.getTitle()+"</title>\n" +
                "</head>\n" +
                "<body>"+news .getContent()+"</body>\n" +
                "\t</html>";

        news.setHtml(news.getContent());
        String fileName = UUID.randomUUID().toString();
        try {
            FileUtil.saveFile(html.getBytes(),htmls,fileName+".html");
            String htmlUrl = url+"/html/"+fileName+".html";
            news.setContent(htmlUrl);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        news.setUpdateTime(new Date());
        if(news.getId()==null) {
            news.setCreateTime(new Date());
            iNewsServicel.insert(news);
        }else{
            EntityWrapper<News> ew = new EntityWrapper<>();
            ew.where("id = {0}",news.getId());
            iNewsServicel.update(news,ew);
        }

        return  new Result(true,"成功");
    }



}
